//
//  ImagePreviewCell.m
//  etionUI
//
//  Created by wangjian on 2/12/14.
//  Copyright (c) 2014 GuangZhouXuanWu. All rights reserved.
//

#import "ImagePreviewCell.h"

@implementation CImagePreviewCell
{
    UIView *delView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        [self.contentView addSubview:_imageView];
    }
    return self;
}

- (void)setEditing:(BOOL)editing
{
    [self setEditing:editing animated:NO];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    if (editing == YES && delView == nil)
    {
        const CGFloat delViewSize = 30;
        delView = [[UIView alloc] initWithFrame:CGRectMake((_imageView.width-delViewSize)/2, (_imageView.height-delViewSize)/2, delViewSize, delViewSize)];
        delView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:.7];
        delView.layer.cornerRadius = delViewSize/2;
        
        UIImageView *delImageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map_cancel"]] autorelease];
        delImageView.frame = CGRectMake((delView.width-delImageView.image.size.width)/2, (delView.height-delImageView.image.size.height)/2, delImageView.image.size.width, delImageView.image.size.height);
        [delView addSubview:delImageView];
    }
    
    if (animated == NO)
    {
        if (editing == YES)
        {
            [self.contentView addSubview:delView];
        }
        else
        {
            [delView removeFromSuperview];
        }
    }
    else
    {
        if (editing == YES)
        {
            [self.contentView addSubview:delView];
        }
        delView.transform = editing == YES ? CGAffineTransformMakeScale(.1, .1) : CGAffineTransformMakeScale(1, 1);
        [UIView animateWithDuration:.25 animations:^
        {
            delView.transform = editing == YES ? CGAffineTransformMakeScale(1, 1) : CGAffineTransformMakeScale(.1, .1);
        } completion:^(BOOL finished)
        {
            if (editing == NO)
            {
                [delView removeFromSuperview];
            }
        }];
    }
}

- (void)dealloc
{
    [_imageView release];
    [delView release];
    [super dealloc];
}

@end
